package com.insigma.business.qrcode.intell.util;

import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

public class AdminAreaCodeUtils
{
    public static int getLevel(String str)
    {
        String s = str.replaceAll("0+$", "");
        int length = s.length();
        if (length <= 2)
        {
            return 1;
        }
        if (length <= 4)
        {
            return 2;
        }
        if (length <= 6)
        {
            return 3;
        }
        if (length <= 9)
        {
            return 4;
        }
        if (length <= 12)
        {
            return 5;
        }
        return -1;
    }

    public static int getNextCodeLength(String str) {
        String s = str.replaceAll("0+$", "");
        int length = s.length();
        if (length <= 2)
        {
            return 4;
        }
        if (length <= 4)
        {
            return 6;
        }
        if (length <= 6)
        {
            return 9;
        }
        if (length <= 9)
        {
            return 12;
        }
        return -1;
    }

    public static String cutAreaCode(String deptCode, String deptLevel)
    {
        int level = getLevel(deptCode);
        if (level < Integer.valueOf(deptLevel)) {
            return deptCode;
        }

        int length = getLevelLength(deptLevel);

        return deptCode.substring(0, length);
    }

    public static int getLevelLength(String deptLevel)
    {

        switch (deptLevel)
        {
            case "1":
                return 2;
            case "2":
                return 4;

            case "3":
                return 6;

            case "4":
                return 9;

            case "5":
                return 12;

            default:
                System.out.println("error:");
        }
        return -1;
    }

    public static int getLevelLength(int deptLevel)
    {

        switch (deptLevel)
        {
            case 1:
                return 2;
            case 2:
                return 4;

            case 3:
                return 6;

            case 4:
                return 9;

            case 5:
                return 12;

            default:
                System.out.println("error:");
        }
        return -1;
    }

    public static String getCutdAreaCode(String str)
    {
        String s = str.replaceAll("0+$", "");
        int length = s.length();
        if (length <= 2)
        {
            return str.substring(0, 2);
        }
        if (length <= 4)
        {
            return str.substring(0, 4);
        }
        if (length <= 6)
        {
            return str.substring(0, 6);
        }
        if (length <= 9)
        {
            return str.substring(0, 9);
        }
        if (length <= 12)
        {
            return str.substring(0, 12);
        }
        return "";
    }

    public static String getFatherCode(String str)
    {
        String s = str.replaceAll("0+$", "");
        int length = s.length();

        if (length <= 2)
        {
            return str.substring(0, 2);
        }
        if (length <= 4)
        {
            return str.substring(0, 4);
        }
        if (length <= 6)
        {
            return str.substring(0, 4);
        }
        if (length <= 9)
        {
            return str.substring(0, 6);
        }
        if (length <= 12)
        {
            return str.substring(0, 9);
        }
        return "";
    }

    public static String  fillAreaCode(String str) {
        if (StringUtils.isBlank(str)) {
            return "";
        }

        return StringUtils.rightPad(str, 12, "0");
    }

    public static boolean checkPrefixSame(String src, String target, int length) {
        if (StringUtils.isBlank(src) || StringUtils.isBlank(target)) {
            return false;
        }

        if (src.substring(0, length).equals(target.substring(0, length))) {
            return true;
        }
        return false;
    }

    
    public static boolean checkIsFatherArea(String father, String child) {
        if (Strings.isNullOrEmpty(father) || Strings.isNullOrEmpty(child)) {
            return false;
        }

        String cutdAreaCode = getCutdAreaCode(father);
        return child.startsWith(cutdAreaCode);
    }

    public static String getFilledFatherCode(String adminAreaCode) {
        String fatherCode = getFatherCode(adminAreaCode);
        return fillAreaCode(fatherCode);
    }

    public static String getCutAdminAreaCode(String adminAreaCode, Integer level) {
        int levelLength = getLevelLength(level);
        if (adminAreaCode.length() <= levelLength) {
            return adminAreaCode;
        }
        return adminAreaCode.substring(0, levelLength);
    }

    public static List<String> getAllFatherCodes(String code) {
        List<String> result = Lists.newArrayList();
        result.add(code);
        for (;;) {
            String fatherCode = fillAreaCode(getFatherCode(code));
            if (StringUtils.isNotBlank(fatherCode) && !code.equals(fatherCode)) {
                result.add(fatherCode);
                code = fatherCode;
            } else {
                break;
            }
        }
        return result;
    }

    public static void main(String[] args) {
        System.out.println(getAllFatherCodes("330211100000"));
    }
}
